package com.sihai.ehr.job.impl;

import com.sihai.core.DataAccessor;
import com.sihai.ehr.model.biz.Duty;
import com.sihai.ehr.model.biz.ExamQuanItemSet;
import com.sihai.ehr.model.biz.MessageType;
import com.sihai.ehr.model.sys.ExaminePeriodEnum;
import com.sihai.ehr.repository.AuditingRepository;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.List;

/**
 * author: wxn
 * since: 13-5-25 下午10:03
 */
public class SendMessageJob {
    @Resource
    private DataAccessor dataAccessor;
    @Resource
    private AuditingRepository auditingRepository;

    //@Property("server.domain.url")
    //private String server_domain;

    //定时器执行的方法
    public void execute() {
        System.out.println("wxn----------execute send message job");
        sendExamQuanAuditMessage();
    }

    //发送定量考核待审核消息
    private void sendExamQuanAuditMessage(){
        //得到定量考核项
        List<ExamQuanItemSet> examQuanItems = dataAccessor.findByHql(ExamQuanItemSet.class, "from ExamQuanItemSet e order by e.examQuanSet.id", null, null, null);
        if(examQuanItems != null && !examQuanItems.isEmpty()){
            int month = Calendar.getInstance().get(Calendar.MONTH)+1;
            boolean isRemindBimonthly = (month % 2 == 1); //是否提醒双月考核;
            boolean isRemindSeason = (month == 1 || month == 4  || month == 7  || month == 10); //是否提醒季考核
            boolean isRemindSemiyearly = (month == 1 || month == 7); //是否提醒半年考核
            boolean isRemindAnnually = (month == 1); //是否提醒年考核
            for(ExamQuanItemSet item : examQuanItems){
                String periodName = null;
                if(item.getPeriodType() == ExaminePeriodEnum.monthly){
                    periodName = ExaminePeriodEnum.monthly.getName();
                }else if(item.getPeriodType() == ExaminePeriodEnum.bimonthly && isRemindBimonthly){
                    periodName = ExaminePeriodEnum.bimonthly.getName();
                }else if(item.getPeriodType() == ExaminePeriodEnum.season && isRemindSeason){
                    periodName = ExaminePeriodEnum.season.getName();
                }else if(item.getPeriodType() == ExaminePeriodEnum.semiyearly && isRemindSemiyearly){
                    periodName = ExaminePeriodEnum.semiyearly.getName();
                }else if(item.getPeriodType() == ExaminePeriodEnum.annually && isRemindAnnually){
                    periodName = ExaminePeriodEnum.annually.getName();
                }

                if(periodName != null){
                    //MessageType type, String targetUrl, Long targetId, Duty duty, final EmployeeinfoWork work, String[] data
                    //发送定量数据输入消息
                    String[] inputData = {MessageType.exam_quan_input.getName(), periodName};
                    auditingRepository.saveMessage(MessageType.exam_quan_input, "exam/quan_input_main.xhtml", item.getId(), item.getDataEntryDuty(), item.getDataEntrier(), inputData);

                    //发送定量数据审核消息
                    String[] auditData = {MessageType.exam_quan_audit.getName(), periodName};
                    auditingRepository.saveMessage(MessageType.exam_quan_audit, "exam/quan_audit_main.xhtml", item.getId(), item.getDataEntryDuty(), item.getDataEntrier(), auditData);
                }
            }
        }
    }
}
